package com.projectxxxxxxxxxx.animator;

import java.util.TimerTask;

import android.content.Context;
import android.view.View;
import android.widget.RemoteViews;

import com.projectxxxxxxxxxx.Constants;
import com.projectxxxxxxxxxx.R;
import com.projectxxxxxxxxxx.screens.HardwareScreen.LabelParams;
import com.projectxxxxxxxxxx.screens.Screen;
import com.projectxxxxxxxxxx.utils.ScheduledTask;
import com.projectxxxxxxxxxx.widget.WidgetView;

public class DoorAnimator extends Animator {

    private final long WIDGET_UPDATE_INTERVAL = 15; // 30
    public static boolean openDoor = false;

    private final int HOW_MUCH_ADD_TO_OPEN_DOOR = 6;

    private final int[] viewsIds = {
            R.id.door2,
            R.id.door3,
            R.id.door4,
            R.id.door5,
            R.id.door6,
            R.id.door7,
            R.id.door8,
            R.id.door9,
            R.id.door10,
            R.id.door11,
            R.id.door12,
            R.id.door13,
            R.id.door14,
            R.id.door15,
            R.id.door16,
            R.id.door17,
            R.id.door18,
            R.id.door19,
            R.id.door20,
            R.id.door21,
            R.id.door22,
            R.id.door23,
            R.id.door24,
            R.id.door25,
            R.id.door26,
            R.id.door27,
            R.id.door28,
            R.id.door29,
            R.id.door30,
            R.id.door31
    };

    private final int[] labelIds = {
            R.id.robo_label_2,
            R.id.robo_label_3,
            R.id.robo_label_4,
            R.id.robo_label_5,
            R.id.robo_label_6,
            R.id.robo_label_7,
            R.id.robo_label_8,
            R.id.robo_label_9,
            R.id.robo_label_10,
            R.id.robo_label_11,
            R.id.robo_label_12,
            R.id.robo_label_13,
            R.id.robo_label_14,
            R.id.robo_label_16,
            R.id.robo_label_17,
            R.id.robo_label_18,
            R.id.robo_label_19,
            R.id.robo_label_20,
            R.id.robo_label_21,
            R.id.robo_label_22,
            R.id.robo_label_23,
            R.id.robo_label_24,
            R.id.robo_label_24
    };


    public DoorAnimator(final Context context, final RemoteViews views) {
        super(context, views);

        if (openDoor) {
            animatorStep = viewsIds.length - 1;
        } else {
            animatorStep = 0;
        }
    }

    @Override
    public void animate(final RemoteViews views, final LabelParams label) {
        
        if (openDoor) {
            animatorStep -= 1;
        } else {
            animatorStep += 1;
        }

        if (animatorStep == viewsIds.length - HOW_MUCH_ADD_TO_OPEN_DOOR) {
            changeToRealScreen(views);
        }

        // stop animation
        if (animatorStep >= viewsIds.length || animatorStep < 0) {

            pingTimer.cancel();

            if (!openDoor) {
                changeToRealScreen(views);
            }

            WidgetView.animator = null;
            animatorStep = 0;

            openDoor = !openDoor;
            
            ////////////////////////////////////////////////////////////////
            if(WidgetView.helpTwoVisible && !WidgetView.helpOneVisible && DoorAnimator.openDoor){
            	views.setViewVisibility(R.id.robo_help_two, View.VISIBLE);
            } else {
            	views.setViewVisibility(R.id.robo_help_two, View.GONE);
            }

            return;
        }

        if (openDoor) {
            views.setViewVisibility(viewsIds[animatorStep + 1], View.GONE);
            views.setViewVisibility(viewsIds[animatorStep], View.VISIBLE);
        } else {
            views.setViewVisibility(viewsIds[animatorStep - 1], View.GONE);
            views.setViewVisibility(viewsIds[animatorStep], View.VISIBLE);
        }

        if (animatorStep < 23) {
            if (label != null) {
                if (animatorStep < 10) {
                    views.setImageViewResource(labelIds[animatorStep],
                            label.getimageResId());
                } else {
                    views.setImageViewResource(labelIds[animatorStep],
                            label.getimageResIdRotate());
                }
            }
        }

        // every time need start timer
        pingTimer = new ScheduledTask.DefaultScheduledTask();
        pingTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                requestRedraw(Constants.ACTION_ANIMATE);
            }
        }, WIDGET_UPDATE_INTERVAL);

    }

    // change frame layout to linear layout
    private void changeToRealScreen(final RemoteViews views) {
        views.setViewVisibility(screenIds[Screen.currentScreen], View.VISIBLE);
        // views.setViewVisibility(R.id.open_door, View.VISIBLE);
    }
}
